package cn.itcast.solrdemo;



import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServer;

import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import cn.itcast.pojo.Item;

public class SolrBean {
	@Test
	public void testCreateIndexBean() throws  Exception{
		
		//连接服务器
		SolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
		//创建一个实体类
		Item item = new Item();
		item.setId("101");
		item.setTitle("荣耀");
		item.setPrice(199900L);
		//将对象提交到server
		server.addBean(item);
		//提交请求
		server.commit();
		
	}
	@Test
	public void testQueryBean() throws Exception{
		//连接Solr服务器
		SolrServer server =  new HttpSolrServer("http://localhost:8080/solr/core2");
		//创建查询对象
		SolrQuery solrQuery = new SolrQuery("price:[139000 TO 299900]");
		//排序
		solrQuery.setSort("price",ORDER.desc);
		//执行查询
		QueryResponse response = server.query(solrQuery);
		
		//获取查询结果
		List<Item> list = response.getBeans(Item.class);
		
		System.out.println("本次共搜索到"+list.size()+"条数据");
		for (Item item : list) {
			System.out.println(item);
		}
	}	
	/**
	 * 分页查询
	 * @throws Exception
	 */
	@Test
	public void tesPagetQueryBean() throws Exception{
		//准备分页参数
		int currentPage = 2; //当前页
		final int PAGE_SIZE = 5; //每页显示条数
		int start = (currentPage-1)*PAGE_SIZE; //当前页的起始条数
		
		
		//连接Solr服务器
		SolrServer server =  new HttpSolrServer("http://localhost:8080/solr/core2");
		//创建查询对象
		SolrQuery solrQuery = new SolrQuery("title:手机");
		//排序
		//solrQuery.setSort("price",ORDER.desc);
		//设置分页信息到查询对象中
		solrQuery.setStart(start);
		solrQuery.setRows(PAGE_SIZE);
		//执行查询
		QueryResponse response = server.query(solrQuery);
		//获取查询结果
		List<Item> list = response.getBeans(Item.class);
		
		System.out.println("本次共搜索到"+list.size()+"条数据");
		System.out.println("当前页:"+currentPage+"页,本页共:"+PAGE_SIZE+"条数据");
		for (Item item : list) {
			System.out.println(item);
		}
	}	
	@Test
	public void tesHighlighttQueryBean() throws Exception{
		
		
		//连接Solr服务器
		SolrServer server =  new HttpSolrServer("http://localhost:8080/solr/core2");
		//创建查询对象
		SolrQuery solrQuery = new SolrQuery("title:手机");
		//设置高亮的标签
		solrQuery.setHighlightSimplePre("<em>");
		solrQuery.setHighlightSimplePost("</em>");
		//设置高亮的字段
		solrQuery.addHighlightField("title");
		//执行查询
		QueryResponse response = server.query(solrQuery);
		//获取查询结果
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		//System.out.println(highlighting);
		List<Item> list = response.getBeans(Item.class);
		for (Item item : list) {
			String id = item.getId();
			System.out.println("id:"+id);
			System.out.println("title:"+highlighting.get(id).get("title").get(0));
			System.out.println("price"+item.getPrice());
		}
	}	
}
